﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using bookMaster.AssetSystem.Entities;
using bookMaster.AssetSystem.Data;

#endregion

namespace bookMaster.AssetSystem.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="RecursosProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class RecursosProviderBaseCore : EntityProviderBase<bookMaster.AssetSystem.Entities.Recursos, bookMaster.AssetSystem.Entities.RecursosKey>
	{		
		#region Get from Many To Many Relationship Functions
		#region GetByIdUsuarioFromUsuariosRecursos
		
		/// <summary>
		///		Gets Recursos objects from the datasource by IdUsuario in the
		///		UsuariosRecursos table. Table Recursos is related to table Usuarios
		///		through the (M:N) relationship defined in the UsuariosRecursos table.
		/// </summary>
		/// <param name="_idUsuario"></param>
		/// <returns>Returns a typed collection of Recursos objects.</returns>
		public TList<Recursos> GetByIdUsuarioFromUsuariosRecursos(string _idUsuario)
		{
			int count = -1;
			return GetByIdUsuarioFromUsuariosRecursos(null,_idUsuario, 0, int.MaxValue, out count);
			
		}
		
		/// <summary>
		///		Gets bookMaster.AssetSystem.Entities.Recursos objects from the datasource by IdUsuario in the
		///		UsuariosRecursos table. Table Recursos is related to table Usuarios
		///		through the (M:N) relationship defined in the UsuariosRecursos table.
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_idUsuario"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of Recursos objects.</returns>
		public TList<Recursos> GetByIdUsuarioFromUsuariosRecursos(string _idUsuario, int start, int pageLength)
		{
			int count = -1;
			return GetByIdUsuarioFromUsuariosRecursos(null, _idUsuario, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets Recursos objects from the datasource by IdUsuario in the
		///		UsuariosRecursos table. Table Recursos is related to table Usuarios
		///		through the (M:N) relationship defined in the UsuariosRecursos table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idUsuario"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Recursos objects.</returns>
		public TList<Recursos> GetByIdUsuarioFromUsuariosRecursos(TransactionManager transactionManager, string _idUsuario)
		{
			int count = -1;
			return GetByIdUsuarioFromUsuariosRecursos(transactionManager, _idUsuario, 0, int.MaxValue, out count);
		}
		
		
		/// <summary>
		///		Gets Recursos objects from the datasource by IdUsuario in the
		///		UsuariosRecursos table. Table Recursos is related to table Usuarios
		///		through the (M:N) relationship defined in the UsuariosRecursos table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idUsuario"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Recursos objects.</returns>
		public TList<Recursos> GetByIdUsuarioFromUsuariosRecursos(TransactionManager transactionManager, string _idUsuario,int start, int pageLength)
		{
			int count = -1;
			return GetByIdUsuarioFromUsuariosRecursos(transactionManager, _idUsuario, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets Recursos objects from the datasource by IdUsuario in the
		///		UsuariosRecursos table. Table Recursos is related to table Usuarios
		///		through the (M:N) relationship defined in the UsuariosRecursos table.
		/// </summary>
		/// <param name="_idUsuario"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Recursos objects.</returns>
		public TList<Recursos> GetByIdUsuarioFromUsuariosRecursos(string _idUsuario,int start, int pageLength, out int count)
		{
			
			return GetByIdUsuarioFromUsuariosRecursos(null, _idUsuario, start, pageLength, out count);
		}


		/// <summary>
		///		Gets Recursos objects from the datasource by IdUsuario in the
		///		UsuariosRecursos table. Table Recursos is related to table Usuarios
		///		through the (M:N) relationship defined in the UsuariosRecursos table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <param name="_idUsuario"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of Recursos objects.</returns>
		public abstract TList<Recursos> GetByIdUsuarioFromUsuariosRecursos(TransactionManager transactionManager,string _idUsuario, int start, int pageLength, out int count);
		
		#endregion GetByIdUsuarioFromUsuariosRecursos
		
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, bookMaster.AssetSystem.Entities.RecursosKey key)
		{
			return Delete(transactionManager, key.IdRecurso);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="_idRecurso">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(int _idRecurso)
		{
			return Delete(null, _idRecurso);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idRecurso">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, int _idRecurso);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Recursos_TipoRecurso key.
		///		FK_Recursos_TipoRecurso Description: 
		/// </summary>
		/// <param name="_tipoRecurso"></param>
		/// <returns>Returns a typed collection of bookMaster.AssetSystem.Entities.Recursos objects.</returns>
		public TList<Recursos> GetByTipoRecurso(int _tipoRecurso)
		{
			int count = -1;
			return GetByTipoRecurso(_tipoRecurso, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Recursos_TipoRecurso key.
		///		FK_Recursos_TipoRecurso Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_tipoRecurso"></param>
		/// <returns>Returns a typed collection of bookMaster.AssetSystem.Entities.Recursos objects.</returns>
		/// <remarks></remarks>
		public TList<Recursos> GetByTipoRecurso(TransactionManager transactionManager, int _tipoRecurso)
		{
			int count = -1;
			return GetByTipoRecurso(transactionManager, _tipoRecurso, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_Recursos_TipoRecurso key.
		///		FK_Recursos_TipoRecurso Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_tipoRecurso"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of bookMaster.AssetSystem.Entities.Recursos objects.</returns>
		public TList<Recursos> GetByTipoRecurso(TransactionManager transactionManager, int _tipoRecurso, int start, int pageLength)
		{
			int count = -1;
			return GetByTipoRecurso(transactionManager, _tipoRecurso, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Recursos_TipoRecurso key.
		///		fkRecursosTipoRecurso Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_tipoRecurso"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of bookMaster.AssetSystem.Entities.Recursos objects.</returns>
		public TList<Recursos> GetByTipoRecurso(int _tipoRecurso, int start, int pageLength)
		{
			int count =  -1;
			return GetByTipoRecurso(null, _tipoRecurso, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Recursos_TipoRecurso key.
		///		fkRecursosTipoRecurso Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_tipoRecurso"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of bookMaster.AssetSystem.Entities.Recursos objects.</returns>
		public TList<Recursos> GetByTipoRecurso(int _tipoRecurso, int start, int pageLength,out int count)
		{
			return GetByTipoRecurso(null, _tipoRecurso, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Recursos_TipoRecurso key.
		///		FK_Recursos_TipoRecurso Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_tipoRecurso"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of bookMaster.AssetSystem.Entities.Recursos objects.</returns>
		public abstract TList<Recursos> GetByTipoRecurso(TransactionManager transactionManager, int _tipoRecurso, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override bookMaster.AssetSystem.Entities.Recursos Get(TransactionManager transactionManager, bookMaster.AssetSystem.Entities.RecursosKey key, int start, int pageLength)
		{
			return GetByIdRecurso(transactionManager, key.IdRecurso, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_Recursos index.
		/// </summary>
		/// <param name="_idRecurso"></param>
		/// <returns>Returns an instance of the <see cref="bookMaster.AssetSystem.Entities.Recursos"/> class.</returns>
		public bookMaster.AssetSystem.Entities.Recursos GetByIdRecurso(int _idRecurso)
		{
			int count = -1;
			return GetByIdRecurso(null,_idRecurso, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Recursos index.
		/// </summary>
		/// <param name="_idRecurso"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="bookMaster.AssetSystem.Entities.Recursos"/> class.</returns>
		public bookMaster.AssetSystem.Entities.Recursos GetByIdRecurso(int _idRecurso, int start, int pageLength)
		{
			int count = -1;
			return GetByIdRecurso(null, _idRecurso, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Recursos index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idRecurso"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="bookMaster.AssetSystem.Entities.Recursos"/> class.</returns>
		public bookMaster.AssetSystem.Entities.Recursos GetByIdRecurso(TransactionManager transactionManager, int _idRecurso)
		{
			int count = -1;
			return GetByIdRecurso(transactionManager, _idRecurso, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Recursos index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idRecurso"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="bookMaster.AssetSystem.Entities.Recursos"/> class.</returns>
		public bookMaster.AssetSystem.Entities.Recursos GetByIdRecurso(TransactionManager transactionManager, int _idRecurso, int start, int pageLength)
		{
			int count = -1;
			return GetByIdRecurso(transactionManager, _idRecurso, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Recursos index.
		/// </summary>
		/// <param name="_idRecurso"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="bookMaster.AssetSystem.Entities.Recursos"/> class.</returns>
		public bookMaster.AssetSystem.Entities.Recursos GetByIdRecurso(int _idRecurso, int start, int pageLength, out int count)
		{
			return GetByIdRecurso(null, _idRecurso, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Recursos index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idRecurso"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="bookMaster.AssetSystem.Entities.Recursos"/> class.</returns>
		public abstract bookMaster.AssetSystem.Entities.Recursos GetByIdRecurso(TransactionManager transactionManager, int _idRecurso, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a TList&lt;Recursos&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="TList&lt;Recursos&gt;"/></returns>
		public static TList<Recursos> Fill(IDataReader reader, TList<Recursos> rows, int start, int pageLength)
		{
			NetTiersProvider currentProvider = DataRepository.Provider;
            bool useEntityFactory = currentProvider.UseEntityFactory;
            bool enableEntityTracking = currentProvider.EnableEntityTracking;
            LoadPolicy currentLoadPolicy = currentProvider.CurrentLoadPolicy;
			Type entityCreationFactoryType = currentProvider.EntityCreationalFactoryType;
			
			// advance to the starting row
			for (int i = 0; i < start; i++)
			{
				if (!reader.Read())
				return rows; // not enough rows, just return
			}
			for (int i = 0; i < pageLength; i++)
			{
				if (!reader.Read())
					break; // we are done
					
				string key = null;
				
				bookMaster.AssetSystem.Entities.Recursos c = null;
				if (useEntityFactory)
				{
					key = new System.Text.StringBuilder("Recursos")
					.Append("|").Append((int)reader[((int)RecursosColumn.IdRecurso - 1)]).ToString();
					c = EntityManager.LocateOrCreate<Recursos>(
					key.ToString(), // EntityTrackingKey
					"Recursos",  //Creational Type
					entityCreationFactoryType,  //Factory used to create entity
					enableEntityTracking); // Track this entity?
				}
				else
				{
					c = new bookMaster.AssetSystem.Entities.Recursos();
				}
				
				if (!enableEntityTracking ||
					c.EntityState == EntityState.Added ||
					(enableEntityTracking &&
					
						(
							(currentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
							(currentLoadPolicy == LoadPolicy.DiscardChanges && c.EntityState != EntityState.Unchanged)
						)
					))
				{
					c.SuppressEntityEvents = true;
					c.IdRecurso = (int)reader[((int)RecursosColumn.IdRecurso - 1)];
					c.LlaveRecurso = (string)reader[((int)RecursosColumn.LlaveRecurso - 1)];
					c.NombreRecurso = (reader.IsDBNull(((int)RecursosColumn.NombreRecurso - 1)))?null:(string)reader[((int)RecursosColumn.NombreRecurso - 1)];
					c.DescripcionRecurso = (reader.IsDBNull(((int)RecursosColumn.DescripcionRecurso - 1)))?null:(string)reader[((int)RecursosColumn.DescripcionRecurso - 1)];
					c.TipoRecurso = (int)reader[((int)RecursosColumn.TipoRecurso - 1)];
					c.PadreRecurso = (reader.IsDBNull(((int)RecursosColumn.PadreRecurso - 1)))?null:(string)reader[((int)RecursosColumn.PadreRecurso - 1)];
					c.EntityTrackingKey = key;
					c.AcceptChanges();
					c.SuppressEntityEvents = false;
				}
				rows.Add(c);
			}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="bookMaster.AssetSystem.Entities.Recursos"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="bookMaster.AssetSystem.Entities.Recursos"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, bookMaster.AssetSystem.Entities.Recursos entity)
		{
			if (!reader.Read()) return;
			
			entity.IdRecurso = (int)reader[((int)RecursosColumn.IdRecurso - 1)];
			entity.LlaveRecurso = (string)reader[((int)RecursosColumn.LlaveRecurso - 1)];
			entity.NombreRecurso = (reader.IsDBNull(((int)RecursosColumn.NombreRecurso - 1)))?null:(string)reader[((int)RecursosColumn.NombreRecurso - 1)];
			entity.DescripcionRecurso = (reader.IsDBNull(((int)RecursosColumn.DescripcionRecurso - 1)))?null:(string)reader[((int)RecursosColumn.DescripcionRecurso - 1)];
			entity.TipoRecurso = (int)reader[((int)RecursosColumn.TipoRecurso - 1)];
			entity.PadreRecurso = (reader.IsDBNull(((int)RecursosColumn.PadreRecurso - 1)))?null:(string)reader[((int)RecursosColumn.PadreRecurso - 1)];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="bookMaster.AssetSystem.Entities.Recursos"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="bookMaster.AssetSystem.Entities.Recursos"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, bookMaster.AssetSystem.Entities.Recursos entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.IdRecurso = (int)dataRow["IdRecurso"];
			entity.LlaveRecurso = (string)dataRow["LlaveRecurso"];
			entity.NombreRecurso = Convert.IsDBNull(dataRow["NombreRecurso"]) ? null : (string)dataRow["NombreRecurso"];
			entity.DescripcionRecurso = Convert.IsDBNull(dataRow["DescripcionRecurso"]) ? null : (string)dataRow["DescripcionRecurso"];
			entity.TipoRecurso = (int)dataRow["TipoRecurso"];
			entity.PadreRecurso = Convert.IsDBNull(dataRow["PadreRecurso"]) ? null : (string)dataRow["PadreRecurso"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="bookMaster.AssetSystem.Entities.Recursos"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">bookMaster.AssetSystem.Entities.Recursos Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		public override void DeepLoad(TransactionManager transactionManager, bookMaster.AssetSystem.Entities.Recursos entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region TipoRecursoSource	
			if (CanDeepLoad(entity, "TiposRecursos|TipoRecursoSource", deepLoadType, innerList) 
				&& entity.TipoRecursoSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.TipoRecurso;
				TiposRecursos tmpEntity = EntityManager.LocateEntity<TiposRecursos>(EntityLocator.ConstructKeyFromPkItems(typeof(TiposRecursos), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.TipoRecursoSource = tmpEntity;
				else
					entity.TipoRecursoSource = DataRepository.TiposRecursosProvider.GetByTipoRecurso(transactionManager, entity.TipoRecurso);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'TipoRecursoSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.TipoRecursoSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.TiposRecursosProvider.DeepLoad(transactionManager, entity.TipoRecursoSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion TipoRecursoSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			// Deep load child collections  - Call GetByIdRecurso methods when available
			
			#region IdUsuarioUsuariosCollection_From_UsuariosRecursos
			// RelationshipType.ManyToMany
			if (CanDeepLoad(entity, "List<Usuarios>|IdUsuarioUsuariosCollection_From_UsuariosRecursos", deepLoadType, innerList))
			{
				entity.IdUsuarioUsuariosCollection_From_UsuariosRecursos = DataRepository.UsuariosProvider.GetByIdRecursoFromUsuariosRecursos(transactionManager, entity.IdRecurso);			 
		
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'IdUsuarioUsuariosCollection_From_UsuariosRecursos' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.IdUsuarioUsuariosCollection_From_UsuariosRecursos != null)
				{
					deepHandles.Add("IdUsuarioUsuariosCollection_From_UsuariosRecursos",
						new KeyValuePair<Delegate, object>((DeepLoadHandle< Usuarios >) DataRepository.UsuariosProvider.DeepLoad,
						new object[] { transactionManager, entity.IdUsuarioUsuariosCollection_From_UsuariosRecursos, deep, deepLoadType, childTypes, innerList }
					));
				}
			}
			#endregion
			
			
			
			#region UsuariosRecursosCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<UsuariosRecursos>|UsuariosRecursosCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'UsuariosRecursosCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.UsuariosRecursosCollection = DataRepository.UsuariosRecursosProvider.GetByIdRecurso(transactionManager, entity.IdRecurso);

				if (deep && entity.UsuariosRecursosCollection.Count > 0)
				{
					deepHandles.Add("UsuariosRecursosCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<UsuariosRecursos>) DataRepository.UsuariosRecursosProvider.DeepLoad,
						new object[] { transactionManager, entity.UsuariosRecursosCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the bookMaster.AssetSystem.Entities.Recursos object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">bookMaster.AssetSystem.Entities.Recursos instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">bookMaster.AssetSystem.Entities.Recursos Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		public override bool DeepSave(TransactionManager transactionManager, bookMaster.AssetSystem.Entities.Recursos entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region TipoRecursoSource
			if (CanDeepSave(entity, "TiposRecursos|TipoRecursoSource", deepSaveType, innerList) 
				&& entity.TipoRecursoSource != null)
			{
				DataRepository.TiposRecursosProvider.Save(transactionManager, entity.TipoRecursoSource);
				entity.TipoRecurso = entity.TipoRecursoSource.TipoRecurso;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();

			#region IdUsuarioUsuariosCollection_From_UsuariosRecursos>
			if (CanDeepSave(entity.IdUsuarioUsuariosCollection_From_UsuariosRecursos, "List<Usuarios>|IdUsuarioUsuariosCollection_From_UsuariosRecursos", deepSaveType, innerList))
			{
				if (entity.IdUsuarioUsuariosCollection_From_UsuariosRecursos.Count > 0 || entity.IdUsuarioUsuariosCollection_From_UsuariosRecursos.DeletedItems.Count > 0)
				{
					DataRepository.UsuariosProvider.Save(transactionManager, entity.IdUsuarioUsuariosCollection_From_UsuariosRecursos); 
					deepHandles.Add("IdUsuarioUsuariosCollection_From_UsuariosRecursos",
						new KeyValuePair<Delegate, object>((DeepSaveHandle<Usuarios>) DataRepository.UsuariosProvider.DeepSave,
						new object[] { transactionManager, entity.IdUsuarioUsuariosCollection_From_UsuariosRecursos, deepSaveType, childTypes, innerList }
					));
				}
			}
			#endregion 
	
			#region List<UsuariosRecursos>
				if (CanDeepSave(entity.UsuariosRecursosCollection, "List<UsuariosRecursos>|UsuariosRecursosCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(UsuariosRecursos child in entity.UsuariosRecursosCollection)
					{
						if(child.IdRecursoSource != null)
						{
								child.IdRecurso = child.IdRecursoSource.IdRecurso;
						}

						if(child.IdUsuarioSource != null)
						{
								child.IdUsuario = child.IdUsuarioSource.IdUsuario;
						}

					}

					if (entity.UsuariosRecursosCollection.Count > 0 || entity.UsuariosRecursosCollection.DeletedItems.Count > 0)
					{
						//DataRepository.UsuariosRecursosProvider.Save(transactionManager, entity.UsuariosRecursosCollection);
						
						deepHandles.Add("UsuariosRecursosCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< UsuariosRecursos >) DataRepository.UsuariosRecursosProvider.DeepSave,
							new object[] { transactionManager, entity.UsuariosRecursosCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region RecursosChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>bookMaster.AssetSystem.Entities.Recursos</c>
	///</summary>
	public enum RecursosChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>TiposRecursos</c> at TipoRecursoSource
		///</summary>
		[ChildEntityType(typeof(TiposRecursos))]
		TiposRecursos,
	
		///<summary>
		/// Collection of <c>Recursos</c> as ManyToMany for UsuariosCollection_From_UsuariosRecursos
		///</summary>
		[ChildEntityType(typeof(TList<Usuarios>))]
		IdUsuarioUsuariosCollection_From_UsuariosRecursos,

		///<summary>
		/// Collection of <c>Recursos</c> as OneToMany for UsuariosRecursosCollection
		///</summary>
		[ChildEntityType(typeof(TList<UsuariosRecursos>))]
		UsuariosRecursosCollection,
	}
	
	#endregion RecursosChildEntityTypes
	
	#region RecursosFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;RecursosColumn&gt;"/> class
	/// that is used exclusively with a <see cref="Recursos"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class RecursosFilterBuilder : SqlFilterBuilder<RecursosColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the RecursosFilterBuilder class.
		/// </summary>
		public RecursosFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the RecursosFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public RecursosFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the RecursosFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public RecursosFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion RecursosFilterBuilder
	
	#region RecursosParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;RecursosColumn&gt;"/> class
	/// that is used exclusively with a <see cref="Recursos"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class RecursosParameterBuilder : ParameterizedSqlFilterBuilder<RecursosColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the RecursosParameterBuilder class.
		/// </summary>
		public RecursosParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the RecursosParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public RecursosParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the RecursosParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public RecursosParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion RecursosParameterBuilder
	
	#region RecursosSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;RecursosColumn&gt;"/> class
	/// that is used exclusively with a <see cref="Recursos"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class RecursosSortBuilder : SqlSortBuilder<RecursosColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the RecursosSqlSortBuilder class.
		/// </summary>
		public RecursosSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion RecursosSortBuilder
	
} // end namespace
